import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
/**
 * NB rename this class so that it has a unique name on the Robot
 * @author 

 * @version 1.0
 */
public class Robot{ 
    public static void main(String[] aArg) throws Exception{
        /**
         * Making instances of all parts.
         */
        BT bluetooth = new BT();
        Control control = new Control();
        Touch touch = new Touch(control);
        AI robotAI = new AI();
        
        
        /**
         * Main algorithm.
         */
        
            LCD.clear();
            LCD.drawString("Waiting for connection.",0,0);
            LCD.refresh();
            UpdateConnect upCon = new UpdateConnect(bluetooth);
            bluetooth.BTConnect();
            robotAI.tryConnecting = true;            
          
        if(bluetooth.isConnected()) {                      
           /**
            * fetching commands
            */
        	LCD.clear();
            LCD.drawString("Connected", 0, 0);
            LCD.refresh();
            robotAI.tryConnecting = false;
        }
        touch.startListener();
           while(true) {
                try {
                    String inputStr = bluetooth.getInputStream();
                    LCD.clear();
                    LCD.drawString(inputStr,0,0);
                    LCD.refresh();
                    control.setSpeed(450);
                    control.doCommand(inputStr);
                    robotAI.tryConnecting = false;
                } catch(IOException e) {
                    LCD.clear();
                    LCD.drawString("Connection lost.",0,0);
                    LCD.refresh();
                    bluetooth.BTClose();
                    
                    robotAI.startAI(control, touch, bluetooth);
                }
            }      
           
    }
}